package com.aaa.config.aop;


import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

@Component
@Aspect
@Slf4j
public class AOPLog {

    private long start = 0;

    @Pointcut("execution(* com.aaa.controller.*.*(..))")
    public  void  haha(){}
//dwa
    @Before("haha()")
    public  void  before(JoinPoint point){

        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        HttpServletResponse response = requestAttributes.getResponse();

        String addr = request.getRemoteAddr();
        String uri = request.getRequestURI();
        Object[] args = point.getArgs();

        start = System.currentTimeMillis();
        log.info("===================请求的地址是:================="+addr);
        log.info("===================请求的路径是:================="+uri);
        log.info("===================请求的参数是:================="+Arrays.toString(args));

    }

    @AfterReturning("haha()")
    public  void  after(){
        long end = System.currentTimeMillis();
        log.info("=================耗时================="+ (end-start));
        log.info("=================结束了=================");
    }

}
